package com.geekbang.work.kmq.v3.broker.core;

import com.geekbang.work.kmq.v3.KmqMessage;
import lombok.SneakyThrows;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;

public final class Kmq {


    private Integer offset = 0;

    public Kmq(String topic, int capacity) {
        this.topic = topic;
        this.capacity = capacity;
        this.linkedList = new LinkedList();
    }

    private String topic;

    private int capacity;

    private LinkedList<KmqMessage> linkedList = new LinkedList<>();

    public boolean send(KmqMessage message) {
        return linkedList.add(message);
    }

    public boolean send(KmqMessage message, int index) {
        linkedList.add(index, message);
        return true;
    }

    public KmqMessage poll() {
        if (CollectionUtils.isEmpty(linkedList) || offset >= linkedList.size()) {
            return null;
        }
        KmqMessage kmqMessage = linkedList.get(offset);
        offset++;
        return kmqMessage;
    }

    @SneakyThrows
    public KmqMessage poll(Integer offset) {
        this.offset = offset;
        return poll();
    }


}
